package cn.nuc.student.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.nuc.common.utils.PageUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.nuc.student.entity.StudentEntity;
import cn.nuc.student.service.StudentService;
import cn.nuc.common.utils.R;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("student")
public class StudentController {
  @Autowired
  @Qualifier("myRedisTemplate")
  private RedisTemplate redisTemplate;

  @Autowired
  private StudentService studentService;

  private long nextStudentId;

  //redis 学生列表初始化
  @RequestMapping("/init")
  public R init() {
    // 第一次获取列表从mysql数据库中取出存入redis数据库中
    List<StudentEntity> list = studentService.list();
    Iterator iterator = list.iterator();
    for (; iterator.hasNext(); ) {
      StudentEntity student = (StudentEntity) iterator.next();
      String s = "student" + student.getSid();
      redisTemplate.opsForValue().set(s, student);
      redisTemplate.opsForList().rightPush("students", s);
    }
    nextStudentId = redisTemplate.opsForList().size("students");
    return R.ok();
  }
  /**
   * 列表
   */
  @RequestMapping("/list")
  //@RequiresPermissions("student:student:list")
  public R list(@RequestParam Map<String, Object> params) {
    List studentsS = redisTemplate.opsForList().range("students", 0, redisTemplate.opsForList().size("students"));
    List<StudentEntity> studentFirstList = redisTemplate.opsForValue().multiGet(studentsS);
    return R.ok().put("student",studentFirstList);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{sid}")
  //@RequiresPermissions("student:student:info")
  public R info(@PathVariable("sid") String sid) {
//    StudentEntity student = studentService.getById(sid);
    StudentEntity student = (StudentEntity) redisTemplate.opsForValue().get("student"+sid);
    return R.ok().put("student", student);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("student:student:save")
  public R save(@RequestBody StudentEntity student) {
    try{
      StudentEntity studentEntity = (StudentEntity) redisTemplate.opsForValue().get("student" + student.getSid());
      return R.error();
    }catch (Exception e){
      studentService.save(student);
      redisTemplate.opsForValue().set("student"+student.getSid(),student);
      return R.ok();
    }
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("student:student:update")
  public R update(@RequestBody StudentEntity student) {
    studentService.updateById(student);
    redisTemplate.opsForValue().set("student"+student.getSid(),student);
    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete/{sid}")
  //@RequiresPermissions("student:student:delete")
  public R delete(@PathVariable("sid") String sid) {
    studentService.removeById(sid);

    redisTemplate.opsForValue().set("student"+sid,"del",1, TimeUnit.SECONDS);
    redisTemplate.opsForList().remove("students",1,"student"+sid);

    return R.ok();
  }

  /**
   * 登录
   */
  @RequestMapping("/check")
  //@RequiresPermissions("student:student:delete")
  public R check(@RequestBody StudentEntity student, HttpSession session) {
//    int sid = studentService.check(student);
//    if (sid == -1) {
//      return R.error();
//    }
//    StudentEntity studentEntity = studentService.getById(sid);
//    session.setAttribute("id", sid);
//    return R.ok().put("student", studentEntity);
    try{
      StudentEntity studentEntity = (StudentEntity) redisTemplate.opsForValue().get("student" + student.getSid());
      return R.ok().put("student", studentEntity);
    }catch (Exception e){
      return R.error();
    }
  }
}
